package com.tomcat.mesos.tomcat.executor;

import com.tomcat.mesos.tomcat.LaunchTomcat;
import com.tomcat.mesos.tomcat.impl.LaunchTomcatImpl;
import org.apache.mesos.Executor;
import org.apache.mesos.ExecutorDriver;
import org.apache.mesos.MesosExecutorDriver;
import org.apache.mesos.Protos;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

/**
 * tomcat 执行器
 * @author Rayn on 2015/12/22 16:46
 * @email liuwei412552703@163.com
 */
public class TomcatExecutor implements Executor
{
    private static final Logger LOG = LoggerFactory.getLogger(TomcatExecutor.class);

    private LaunchTomcat tomcat = null;


    @Override
    public void registered(ExecutorDriver driver, Protos.ExecutorInfo executorInfo, Protos.FrameworkInfo frameworkInfo, Protos.SlaveInfo slaveInfo)
    {
        LOG.info("向 Slave 注册一个 Executor ." + slaveInfo.getHostname());

        tomcat = new LaunchTomcatImpl(new File("/home/hadoop/test"));
        tomcat.init();


    }

    /**
     *
     * @param driver
     * @param slaveInfo
     */
    @Override
    public void reregistered(ExecutorDriver driver, Protos.SlaveInfo slaveInfo)
    {
        if(null == tomcat){
            tomcat = new LaunchTomcatImpl(new File("/home/hadoop/test"));
        }

    }

    /**
     *
     * @param driver
     */
    @Override
    public void disconnected(ExecutorDriver driver)
    {
        LOG.info("从 slave 机子上断开连接。");




    }

    /**
     * 运行任务
     * @param driver
     * @param task
     */
    @Override
    public void launchTask(ExecutorDriver driver, Protos.TaskInfo task)
    {
        LOG.info("开始运行任务");
        Builder status = TaskStatus.newBuilder().setTaskId(task.getTaskId());

        /** Notify Mesos that the task is now running **/
        status.setState(TaskState.TASK_RUNNING);
        driver.sendStatusUpdate(status.build());

        tomcat.start();


    }

    /**
     * 停止任务
     * @param driver
     * @param taskId
     */
    @Override
    public void killTask(ExecutorDriver driver, Protos.TaskID taskId)
    {
        LOG.info("停止任务");





    }

    /**
     *
     * @param driver
     * @param data
     */
    @Override
    public void frameworkMessage(ExecutorDriver driver, byte[] data)
    {
        LOG.info("frameworkMessage method invoke.");

    }

    /**
     * 关闭任务
     * @param driver
     */
    @Override
    public void shutdown(ExecutorDriver driver)
    {
        LOG.info("停止这个 Executor （执行器）");



    }

    /**
     * 发生错误
     * @param driver
     * @param message
     */
    @Override
    public void error(ExecutorDriver driver, String message)
    {

    }

    /**
     * tomcat 运行启动入口
     * @param args
     */
    public static void main(String[] args)
    {
        TomcatExecutor tomcatExecutor = new TomcatExecutor();

        MesosExecutorDriver driver = new MesosExecutorDriver(tomcatExecutor);
        Protos.Status status = driver.run();

        LOG.info("Tomcat Executor exited. exit status : " + status);

    }
}
